package com.moorecat.app;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebIconDatabase;
import android.widget.ImageView;
import android.widget.TextView;

import com.moorecat.R;
import com.moorecat.bean.GlodenManual;
import com.moorecat.http.HttpUtils;
import com.moorecat.utils.DateUtils;

/**
 * @author pengfei.xu
 * @version 创建时间：2011-6-21 下午09:05:11 类说明
 */
public class BusinessDynamicMangazine extends Activity {

    private TextView title;
	private TextView content;
    private TextView date;
    private TextView number;
    private ImageView leftImage;

    private ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.business_dynamic_mangazine);
        title = (TextView) findViewById(R.id.m_title);
        leftImage = (ImageView) findViewById(R.id.left_image);
        number = (TextView) findViewById(R.id.number);
        date = (TextView) findViewById(R.id.date);
		content = (TextView) findViewById(R.id.business_list_name);
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage(getString(R.string.loading));
        progressDialog.show();
		new DynamicMangazineTask().execute("");
	}

	private class DynamicMangazineTask extends
			AsyncTask<String, Integer, GlodenManual> {
		@Override
		protected GlodenManual doInBackground(String... params) {
			GlodenManual glodenManual = null;
			try {
				glodenManual = HttpUtils.fetchLatestGlodenManual("0");
			} catch (Exception e) {
				e.printStackTrace();
			}
			return glodenManual;
		}

		@Override
		protected void onPostExecute(GlodenManual result) {
			super.onPostExecute(result);
            if(result != null){
                title.setText(result.getTitle());
                String d = DateUtils.long2Short(result.getStartDate());
                date.setText(d);
                number.setText("NO."+result.getVersion());
                content.setText("主要商户：\n" + result.getContent());
                leftImage.setImageResource(R.drawable.mangezine_pic);
                if(result.getTopImage() != null && !"".equals(result.getTopImage().trim())){
                    new BitmapTask().execute(result.getTopImage());
                }
                progressDialog.dismiss();
            }
		}

	}

    private class BitmapTask extends AsyncTask<String, Integer, Bitmap>{

        @Override
        protected Bitmap doInBackground(String... urls) {
            Bitmap icon=null;
            try{
               icon = HttpUtils.downloadBitmap(urls[0]);
            }catch (Exception e){
                e.printStackTrace();
            }
           return  icon;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            if(bitmap!=null){
                leftImage.setImageBitmap(bitmap);
            }
        }
    }

}
